package com.noyes4j.auth.controller;

import com.noyes4j.auth.form.AirkissConfigForm;
import com.noyes4j.auth.service.SysDeviceService;
import com.noyes4j.common.core.domain.R;
import com.noyes4j.common.utils.qr.QRCodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RestController
public class HandleDeviceController
{

    @Autowired
    SysDeviceService sysDeviceService;

    private static Logger logger = LoggerFactory.getLogger(HandleDeviceController.class);


    @PostMapping("wxAirKiss/getAirKissConfigData")
    public R getAirKissConfigData(@RequestBody AirkissConfigForm airkissConfigForm){
        return sysDeviceService.getAirKissConfigData(airkissConfigForm.getWifiSsid(),airkissConfigForm.getWifiPwd(),airkissConfigForm.getJsCode());
    }

    @GetMapping("wxAirKiss/getAirKissQrCode")
    public void getAirKissQrCode(@RequestParam("path") String path, HttpServletResponse response){
        try{
            if(path!=null&&!"".equals(path)){
                path = QRCodeUtil.iotQrCodeBasePath+path;
                File file = new File(path);
                if(file.exists()){ //判断文件父目录是否存在
                    //提取文件名
                    int index = path.lastIndexOf("/")<0?0:path.lastIndexOf("/");
                    String filename=path.substring(index,path.length());

                    response.setContentType("img/jpg");
                    response.setCharacterEncoding("utf-8");
                    byte[] buffer = new byte[1024];
                    FileInputStream fis = null; //文件输入流
                    BufferedInputStream bis = null;

                    OutputStream os = null; //输出流
                    try {
                        os = response.getOutputStream();
                        fis = new FileInputStream(file);
                        bis = new BufferedInputStream(fis);
                        int length;
                        while ((length = bis.read(buffer)) > 0)
                        {
                            os.write(buffer, 0, length);
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        logger.error(e.getMessage());
                    }
                    try {
                        bis.close();
                        fis.close();
                        file.delete();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        logger.error(e.getMessage());
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }

    @PostMapping("wxAirKiss/bindDeviceWithWechat")
    public R bindDeviceWithWechat(@RequestParam("sn") String sn,@RequestParam("openid") String openid){
        return sysDeviceService.bindDeviceWithWechat(sn,openid);
    }

    @GetMapping("wxAirKiss/getTest")
    public R getTest(String msg){
        return sysDeviceService.getTest(msg);
    }

    @PostMapping("wxAirKiss/postTest")
    public R postTest(String msg){
        return sysDeviceService.postTest(msg);
    }
}
